;/*
; * Copyright (c) 2022, IMMORTA Inc.
; * All rights reserved.
; *
; * Redistribution and use in source and binary forms, with or without modification,
; * are permitted provided that the following conditions are met:
; *
; * - Redistributions of source code must retain the above copyright notice, this list
; *   of conditions and the following disclaimer.
; *
; * - Redistributions in binary form must reproduce the above copyright notice, this
; *   list of conditions and the following disclaimer in the documentation and/or
; *   other materials provided with the distribution.
; *
; * - Neither the name of IMMORTA Inc. nor the names of its
; *   contributors may be used to endorse or promote products derived from this
; *   software without specific prior written permission.
; *
; * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
; * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
; * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
; */
;

        MODULE  ?cstartup

        ;; Forward declaration of sections.
        SECTION CSTACK:DATA:NOROOT(3)

        SECTION .intvec:CODE:ROOT(2)

        EXTERN  __iar_program_start
        EXTERN  SystemInit
        EXTERN  init_data_bss
        PUBLIC  __vector_table
        PUBLIC  __Vectors
        PUBLIC  __Vectors_End
        PUBLIC  __Vectors_Size

        DATA

__vector_table
        DCD     sfe(CSTACK)
        DCD     Reset_Handler              ; Reset Handler
        DCD     NMI_Handler                ; NMI Handler
        DCD     HardFault_Handler          ; Hard Fault Handler
        DCD     MemManage_Handler          ; MPU Fault Handler
        DCD     BusFault_Handler           ; Bus Fault Handler
        DCD     UsageFault_Handler         ; Usage Fault Handler
        DCD     0                          ; Reserved
        DCD     0                          ; Reserved
        DCD     0                          ; Reserved
        DCD     0                          ; Reserved
        DCD     SVC_Handler                ; SVCall Handler
        DCD     DebugMon_Handler           ; Debug Monitor Handler
        DCD     0                          ; Reserved
        DCD     PendSV_Handler             ; PendSV Handler
        DCD     SysTick_Handler            ; SysTick Handler

        ; External Interrupts
        DCD     WDG_IRQHandler             ; Internal Watch Dog
        DCD     ERM_Error_IRQHandler       ; ERM Error
        DCD     LVD_IRQHandler             ; Low Voltage Detection
        DCD     CLOCK_Fault_IRQHandler     ; Clock Fault
        DCD     DMA_Channel0_IRQHandler    ; DMA Channel 0
        DCD     DMA_Channel1_IRQHandler    ; DMA Channel 1
        DCD     DMA_Channel2_IRQHandler    ; DMA Channel 2
        DCD     DMA_Channel3_IRQHandler    ; DMA Channel 3
        DCD     DMA_Channel4_IRQHandler    ; DMA Channel 4
        DCD     DMA_Channel5_IRQHandler    ; DMA Channel 5
        DCD     DMA_Channel6_IRQHandler    ; DMA Channel 6
        DCD     DMA_Channel7_IRQHandler    ; DMA Channel 7
        DCD     Reserved0_IRQHandler       ; Reserved0
        DCD     Reserved1_IRQHandler       ; Reserved1
        DCD     Reserved2_IRQHandler       ; Reserved2
        DCD     Reserved3_IRQHandler       ; Reserved3
        DCD     GPIOA_IRQHandler           ; GPIOA
        DCD     GPIOB_IRQHandler           ; GPIOB
        DCD     GPIOC_IRQHandler           ; GPIOC
        DCD     GPIOD_IRQHandler           ; GPIOD
        DCD     GPIOE_IRQHandler           ; GPIOE
        DCD     Reserved4_IRQHandler       ; Reserved4
        DCD     RTC_IRQHandler             ; RTC
        DCD     TIMER0_IRQHandler          ; Timer 0
        DCD     TIMER1_IRQHandler          ; Timer 1
        DCD     TIMER2_IRQHandler          ; Timer 2
        DCD     TIMER3_IRQHandler          ; Timer 3
        DCD     SPI0_IRQHandler            ; SPI 0
        DCD     SPI1_IRQHandler            ; SPI 1
        DCD     SPI2_IRQHandler            ; SPI 2
        DCD     SPI3_IRQHandler            ; SPI 3
        DCD     I2C0_IRQHandler            ; I2C 0
        DCD     I2C1_IRQHandler            ; I2C 1
        DCD     UART0_IRQHandler           ; UART 0
        DCD     UART1_IRQHandler           ; UART 1
        DCD     UART2_IRQHandler           ; UART 2
        DCD     UART3_IRQHandler           ; UART 3
        DCD     Reserved5_IRQHandler       ; Reserved5
        DCD     Reserved6_IRQHandler       ; Reserved6
        DCD     CAN0_IRQHandler            ; CAN 0
        DCD     CAN1_IRQHandler            ; CAN 1
        DCD     CAN2_IRQHandler            ; CAN 2
        DCD     Reserved7_IRQHandler       ; Reserved7
        DCD     Reserved8_IRQHandler       ; Reserved8
        DCD     Reserved9_IRQHandler       ; Reserved9
        DCD     ADC0_IRQHandler            ; ADC 0
        DCD     ADC1_IRQHandler            ; ADC 1
        DCD     CMP0_IRQHandler            ; CMP 0
        DCD     IPWM0_Channel_IRQHandler   ; IPMW 0 Channel
        DCD     IPWM0_Overflow_IRQHandler  ; IPMW 0 Overflow
        DCD     IPWM1_Channel_IRQHandler   ; IPMW 1 Channel
        DCD     IPWM1_Overflow_IRQHandler  ; IPMW 1 Overflow
        DCD     SPWM0_Channel_IRQHandler   ; SPMW 0 Channel
        DCD     SPWM0_Fault_IRQHandler     ; SPMW 0 Fault
        DCD     SPWM0_Overflow_IRQHandler  ; SPMW 0 Overflow
        DCD     SPWM1_Channel_IRQHandler   ; SPMW 1 Channel
        DCD     SPWM1_Fault_IRQHandler     ; SPMW 1 Fault
        DCD     SPWM1_Overflow_IRQHandler  ; SPMW 1 Overflow
        DCD     SPWM2_Channel_IRQHandler   ; SPMW 2 Channel
        DCD     SPWM2_Fault_IRQHandler     ; SPMW 2 Fault
        DCD     SPWM2_Overflow_IRQHandler  ; SPMW 2 Overflow
        DCD     Reserved10_IRQHandler      ; Reserved10
        DCD     MBOX_IRQHandler            ; HSM Mbox
        DCD     HSM_Error_IRQHandler       ; HSM Error
__Vectors_End

__BootHeader
        DCD   0x5AA55AA5;         ;/* Valid boot header marker */
        DCD   0x00000000;         ;/* Boot configuration word */
                                  ;/* bit0: soc wdg enable,0-disable,1-enable */
                                  ;/* bit1~bit31: reserved */
        DCD   0x00000000;         ;/* 0:HSI 1:HSE+PLL 2:HSI+PLL */
        DCD   8000000;            ;/* Frequency of EXTAL/XTAL */
        DCD   0;                  ;/* PLL previous divider, 0~63 */
        DCD   115;                ;/* PLL feedback divider, 0~4095 */
        DCD   2;                  ;/* PLL post divider1, 0~7 */
        DCD   1;                  ;/* PLL post divider2, 0~3 */
        DCD   0;                  ;/* PLL vcodiv enable, 0~1 */
        DCD   0x18000000;         ;/* HSM FW image address */
__BootHeader_End

__Vectors       EQU   __vector_table
__Vectors_Size  EQU   __Vectors_End - __Vectors


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Default interrupt handlers.
;;
        THUMB

        PUBWEAK Reset_Handler
        SECTION .text:CODE:REORDER:NOROOT(2)

Reset_Handler
        CPSID   I    ;Mask interrupts

        LDR R1, = 0x4001002C
        LDR R0, = 0x5A1234A5
        STR R0, [R1]

        LDR R1, = 0x40010020
        LDR R0, = 0xFFFFFFFF
        STR R0, [R1]

        LDR R1, = 0x40010024
        LDR R0, = 0xFFFFFFFF
        STR R0, [R1]

        LDR R1, = 0x40000000
        LDR R0, = 0x34C
        STR R0, [R1]

        ;Errata
        LDR R1, = 0x4000E800
        LDR R0, = 0x00000709
        STR R0, [R1]

        LDR R1, = 0x4000E804
        LDR R0, = 0x00000709
        STR R0, [R1]

        LDR R1, = 0x4000E414
        LDR R0, = 0x00000709
        STR R0, [R1]

        LDR R1, = 0x4000E02C
        LDR R0, = 0x0000070A
        STR R0, [R1]

        LDR R1, = 0x4000E010
        LDR R0, = 0x00000709
        STR R0, [R1]

        LDR R1, = 0x4000E410
        LDR R0, = 0x0000070A
        STR R0, [R1]

        LDR R1, = 0x4000000C
        LDR R0, = 0xB2063001
        STR R0, [R1]

        LDR R1, = 0x40002034
        LDR R0, = 0xE0402008
        STR R0, [R1]

        LDR R1, = 0x4000000C
        LDR R0, = 0xB2063000
        STR R0, [R1]

        ;Init ECC RAM
        IMPORT __RAM_START__, __RAM_END__
        LDR     R1, = __RAM_START__
        LDR     R2, = __RAM_END__
        SUBS    R2, R2, R1
        SUBS    R2, #1
        BLE LE0

        MOVS    R0, #0
        MOVS    R3, #4
LE1
        STR R0, [R1]
        ADD R1, R1, R3
        SUBS R2, #4
        BGE LE1
LE0
        NOP

        CPSIE   I    ; Unmask interrupts

        LDR     R0, =SystemInit
        BLX     R0
        LDR     R0, =__iar_program_start
        BX      R0

        PUBWEAK NMI_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
NMI_Handler
        B .

        PUBWEAK HardFault_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
HardFault_Handler
        B .

        PUBWEAK MemManage_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
MemManage_Handler
        B .
        
        PUBWEAK BusFault_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
BusFault_Handler
        B .
        
        PUBWEAK UsageFault_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
UsageFault_Handler
        B .

        PUBWEAK SVC_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
SVC_Handler
        B .

        PUBWEAK DebugMon_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
DebugMon_Handler
        B .

        PUBWEAK PendSV_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
PendSV_Handler
        B .

        PUBWEAK SysTick_Handler
        SECTION .text:CODE:REORDER:NOROOT(1)
SysTick_Handler
        B .
        
        PUBWEAK    WDG_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
WDG_IRQHandler
        B .
        
        PUBWEAK    ERM_Error_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
ERM_Error_IRQHandler
        B .
        
        PUBWEAK    LVD_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
LVD_IRQHandler
        B .
        
        PUBWEAK    CLOCK_Fault_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
CLOCK_Fault_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel0_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel0_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel1_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel1_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel2_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel2_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel3_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel3_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel4_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel4_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel5_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel5_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel6_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel6_IRQHandler
        B .
        
        PUBWEAK    DMA_Channel7_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
DMA_Channel7_IRQHandler
        B .
        
        PUBWEAK    Reserved0_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved0_IRQHandler
        B .
        
        PUBWEAK    Reserved1_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved1_IRQHandler
        B .
        
        PUBWEAK    Reserved2_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved2_IRQHandler
        B .

        PUBWEAK    Reserved3_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved3_IRQHandler
        B .
        
        PUBWEAK    GPIOA_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
GPIOA_IRQHandler
        B .
        
        PUBWEAK    GPIOB_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
GPIOB_IRQHandler
        B .
        
        PUBWEAK    GPIOC_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
GPIOC_IRQHandler
        B .
        
        PUBWEAK    GPIOD_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
GPIOD_IRQHandler
        B .
        
        PUBWEAK    GPIOE_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
GPIOE_IRQHandler
        B .
        
        PUBWEAK    Reserved4_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved4_IRQHandler
        B .
        
        PUBWEAK    RTC_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
RTC_IRQHandler
        B .
        
        PUBWEAK    TIMER0_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
TIMER0_IRQHandler
        B .
        
        PUBWEAK    TIMER1_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
TIMER1_IRQHandler
        B .
        
        PUBWEAK    TIMER2_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
TIMER2_IRQHandler
        B .
        
        PUBWEAK    TIMER3_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
TIMER3_IRQHandler
        B .
        
        PUBWEAK    SPI0_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
SPI0_IRQHandler
        B .
        
        PUBWEAK    SPI1_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
SPI1_IRQHandler
        B .
        
        PUBWEAK    SPI2_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
SPI2_IRQHandler
        B .
        
        PUBWEAK    SPI3_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
SPI3_IRQHandler
        B .
        
        PUBWEAK    I2C0_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
I2C0_IRQHandler
        B .
        
        PUBWEAK    I2C1_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
I2C1_IRQHandler
        B .
        
        PUBWEAK    UART0_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
UART0_IRQHandler
        B .
        
        PUBWEAK    UART1_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
UART1_IRQHandler
        B .
        
        PUBWEAK    UART2_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
UART2_IRQHandler
        B .
        
        PUBWEAK    UART3_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
UART3_IRQHandler
        B .
        
        PUBWEAK    Reserved5_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved5_IRQHandler
        B .
        
        PUBWEAK    Reserved6_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved6_IRQHandler
        B .
        
        PUBWEAK    CAN0_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
CAN0_IRQHandler
        B .
        
        PUBWEAK    CAN1_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
CAN1_IRQHandler
        B .
        
        PUBWEAK    CAN2_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
CAN2_IRQHandler
        B .
        
        PUBWEAK    Reserved7_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved7_IRQHandler
        B .
        
        PUBWEAK    Reserved8_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved8_IRQHandler
        B .
        
        PUBWEAK    Reserved9_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved9_IRQHandler
        B .
        
        PUBWEAK    ADC0_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
ADC0_IRQHandler
        B .
        
        PUBWEAK    ADC1_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
ADC1_IRQHandler
        B .
        
        PUBWEAK    CMP0_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
CMP0_IRQHandler
        B .
        
        PUBWEAK    IPWM0_Channel_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
IPWM0_Channel_IRQHandler
        B .
        
        PUBWEAK    IPWM0_Overflow_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
IPWM0_Overflow_IRQHandler
        B .
        
        PUBWEAK    IPWM1_Channel_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
IPWM1_Channel_IRQHandler
        B .
        
        PUBWEAK    IPWM1_Overflow_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)
IPWM1_Overflow_IRQHandler
        B .
        
        PUBWEAK    SPWM0_Channel_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM0_Channel_IRQHandler
        B .
        
        PUBWEAK    SPWM0_Fault_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM0_Fault_IRQHandler
        B .
        
        PUBWEAK    SPWM0_Overflow_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM0_Overflow_IRQHandler
        B .
        
        PUBWEAK    SPWM1_Channel_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM1_Channel_IRQHandler
        B .
        
        PUBWEAK    SPWM1_Fault_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM1_Fault_IRQHandler
        B .
        
        PUBWEAK    SPWM1_Overflow_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM1_Overflow_IRQHandler
        B .
        
        PUBWEAK    SPWM2_Channel_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM2_Channel_IRQHandler
        B .
        
        PUBWEAK    SPWM2_Fault_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM2_Fault_IRQHandler
        B .
        
        PUBWEAK    SPWM2_Overflow_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
SPWM2_Overflow_IRQHandler
        B .
        
        PUBWEAK    Reserved10_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
Reserved10_IRQHandler
        B .
        
        PUBWEAK    MBOX_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
MBOX_IRQHandler
        B .
        
        PUBWEAK    HSM_Error_IRQHandler 
        SECTION .text:CODE:REORDER:NOROOT(1)
HSM_Error_IRQHandler
        B .

        END
